<?php
	# Include DB
	require_once ("../../config.php");
	
	# Verify what button was clicked.
	$btaction = $_POST["submit_ccedit"];
	if ($btaction == "Voltar"){
		header("Location: /itcontrol/com/main/cc/cc_list.php");
	}else{
		
		# Array with the values
		$values[0] = $_POST["input_id"];
		$values[1] = $_POST["input_nameCC"];
		$values[2] = $_POST["input_cc"];
		$values[3] = $_POST["input_nameUnit"];
		
		# Count of OptionFields
		$countOptFields = 1;
		
		# Array with fields
		$fields[0] = "id";
		$fields[1] = "nameCC";
		$fields[2] = "cc";
		$fields[3] = "unit_id";
		
		$fieldsOpt[0] = "id";
		
		$namefieldsOpt[0] = "ID";
		
		$whereOpt[0] = "nameUnit = '" .$values[3]. "'";
		
		# Array with Alias from fields
		$namefields[0] = "ID";
		$namefields[1] = "Nome";
		$namefields[2] = "CC";
		$namefields[3] = "Unidade";
		
		# Mount array where which contains the id value
		$where[0] = $fields[0] ."='" .$values[0]. "'";
		
		# Connect with DB
		$dbconnection = new connectDB();
		
		# Check what's the action
		switch ($btaction){
			# Case the pressed button is Atualizar, then
			case "Atualizar":
			
				# Search unit ID
				$query = mysql_query($dbconnection->selectDB("tb_unit", "unit", "update", $fieldsOpt, $namefieldsOpt, $whereOpt));
				$result = mysql_fetch_array($query);
				
				$valuesOpt[0] = $result[0];

				# Run the select command with the previous info
				$query = mysql_query($dbconnection->selectDB("tb_cc", "cc", "update", $fields, $namefields, $where));
				$result = mysql_fetch_array($query);
				$updatefields;			
				$count = 0;
				
				# Identifies which fields has change
				for ($i = 0; $i < count($values) - $countOptFields; $i++) {
					if ($values[$i] <> $result[$i]){
						$updatefields[$count] = $fields[$i]. "='" .$values[$i]. "'";
						$count = $count + 1;
					}
				}
			
				# Identifies if the Unit field has change				
				if ($result[3] <> $valuesOpt[0]){
					$updatefields[$count] = $fields[3]. "='" .$valuesOpt[0]. "'";
				}
				
				$error = FALSE;
		
				# Check if there is one or more empty fields. Having a empty field then change the value of $error and redirect to the add login page with the 
				# previous inserted data
				if (empty($values[0])){
					$error = TRUE;
					echo "0";
				}
				if (empty($values[1])){
					$error = TRUE;
					echo "1";
				}
				if (empty($values[2])){
					$error = TRUE;
					echo "2";
				}
				if (empty($values[3])){
					$error = TRUE;
					echo "3";
				}
				
				
				
				# Store data
				if ($error == FALSE) {
					
					# If doesn't are empty, runs update
					$query = $dbconnection->updateDB("tb_cc", $updatefields, $where);
				}else {
					
					# Else, shows error
					$query = "fail";
				}
					
				# Mount the parameter of redirection
				$query = "cc-upd-" .$query;
				
				# Disconnect with DB
				$dbconnection->disconnectDB();
	
				# Redirect to main page and show the result (positive or negative)
				header("Location: /itcontrol/com/redirect/redirect.php?action=".$query);
				break;
			case "Deletar":
				# Check if exist registry
				$check = $dbconnection->checkDB("tb_cc", "cc", "delete", $fields, $namefields, $where);
				# If exist, do the deletion (This test it's obvious and retarded, but i'll do.
				if ($check <> 0) {
					# Delete registry
					$query = $dbconnection->deleteDB("tb_cc", $where);
					
					# Mount the parameter of redirection
					$query = "cc-del-" .$query;
					
					# Disconnect with DB
					$dbconnection->disconnectDB();
					
					# Redirect to main page and show the result (positive or negative)
					header("Location: /itcontrol/com/redirect/redirect.php?action=".$query);			
				}
				break;
		}
	}
	
?>